//
//  SearchResultViewController.m
//  easystudy
//
//  Created by 张娇娇 on 16/8/30.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "SearchResultViewController.h"
#import "SearchResultTableViewCell.h"
#import "DoctorInfoController.h"
#import "MapViewController.h"
#import "DoctorTimeController.h"
#import "ImageListController.h"

@interface SearchResultViewController ()<UIAlertViewDelegate,UIScrollViewDelegate,UIGestureRecognizerDelegate>
{
    
    __weak IBOutlet UIView *headView;
    __weak IBOutlet UITableView *infoTable;
    UIView *contentView;
    UIPageControl *_pageControl;
    UITapGestureRecognizer *_tap;
    UIView *blackView;
    UIScrollView *_scrollView;
    NSMutableArray *_imageViews;
    NSTimer *_timer;
    NSInteger _add,_imageCount;
}

@end

@implementation SearchResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _add = 1;
    _imageCount = 4;
    headView.alpha = 0.7;
    [self initWithScollView];
    infoTable.tableHeaderView = contentView;
    
}
- (void)initWithScollView
{
    _imageViews = [NSMutableArray array];
    contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, fWidth_Current, 180)];
    
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, fWidth_Current, 180)];
    _scrollView.delegate = self;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.contentSize = CGSizeMake(fWidth_Current * 4, 180);
    [contentView addSubview:_scrollView];
    
    for (int index = 0; index < _imageCount; index ++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(fWidth_Current * index, 0, fWidth_Current, 180)];
        imageView.image = [UIImage imageNamed:@"main_image02"];
        imageView.userInteractionEnabled = YES;
        imageView.tag = 2000+index;
        [_scrollView addSubview:imageView];
        [_imageViews addObject:imageView];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
        tapGesture.delegate = self;
        [imageView addGestureRecognizer:tapGesture];
        

    }
    
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 150, fWidth_Current, 20)];
    _pageControl.numberOfPages = 4;
    _pageControl.currentPage = 0;
    _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    [_pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
    [contentView addSubview:_pageControl];
     _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}
- (void)pageChanged:(UIPageControl *)control
{
     _scrollView.contentOffset = CGPointMake(control.currentPage*fWidth_Current, 0);
}
-(void)onTimer
{
    _pageControl.currentPage += _add;
    if (_pageControl.currentPage == 0 || _pageControl.currentPage == (_imageCount-1))
    {
        _add = -_add;
    }
    _scrollView.contentOffset = CGPointMake(_pageControl.currentPage*fWidth_Current, 0);
}

- (void)tapGesture:(UITapGestureRecognizer *)tap
{
    ImageListController *imageList = [[ImageListController alloc]init];
    imageList.imageList = _imageViews;
    imageList.currentIndex = _pageControl.currentPage;
    [self.navigationController presentViewController:imageList animated:YES completion:nil];
    
}

- (IBAction)shareButton:(id)sender {
    
}
- (IBAction)loginButton:(id)sender {
    
}
- (IBAction)bottomBtn:(id)sender {
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"我们将拨打0514-8760887为您预约" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"拨打", nil];
    [alertView show];
}

- (IBAction)backBtnPressed:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    _pageControl.currentPage = ceil(scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame));
    
}
#pragma mark - UITableView DataSource && Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 180;
    }
    else if (indexPath.row == 1) {
       return 80;
    }
    return 40;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"CELL";
    static NSString *identifier1 = @"CELL1";
    if (indexPath.row == 0) {
        SearchResultTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:identifier1];
        if (!cell1) {
            NSArray *xibArray = [[NSBundle mainBundle] loadNibNamed:@"SearchResultTableViewCell" owner:nil options:nil];
            cell1 = [xibArray objectAtIndex:0];
        }
        cell1.content.text = @"集医疗、科研、教学为一体的大型综合医院。它隶属于中国协和医科大学(2006年改为北京协和医学院/清华大学医学部)，是其的临床医学院，同时也是中国医学科学院的临床医学研究所，中华人民共和国卫生部指定的诊治疑难重症的技术指导中心之一";
        return cell1;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:15.0];
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    cell.textLabel.textColor = [UIColor grayColor];
    if (indexPath.row == 1) {
        cell.imageView.image = [UIImage imageNamed:@"defaultDoctor"];
        cell.textLabel.text = @"主治医师";
    }
    else if (indexPath.row == 2) {
        cell.imageView.image = [UIImage imageNamed:@"image_mapDetail_hosp1"];
        cell.textLabel.text = @"南京市江东中路";
    }
    else {
        cell.imageView.image = [UIImage imageNamed:@"image_opentime1"];
        cell.textLabel.text = @"周一至周日 08:00-16:00";
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
        DoctorInfoController *infoVC = [[DoctorInfoController alloc]initWithNibName:@"DoctorInfoController" bundle:nil
                                        ];
        [self.navigationController pushViewController:infoVC animated:YES];
    
    }
    else if (indexPath.row == 2) {
        MapViewController *mapVC = [[MapViewController alloc] init];
        mapVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:mapVC animated:YES];
    }
    else {
        DoctorTimeController *infoVC = [[DoctorTimeController alloc]initWithNibName:@"DoctorTimeController" bundle:nil
                                        ];
        [self.navigationController pushViewController:infoVC animated:YES];
    }
}
#pragma mark -- UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        NSString *tel = @"0514-8760887";
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[@"telprompt://" stringByAppendingString:[[tel componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]] componentsJoinedByString:@""]]]];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
